/**
 * 作者: wgx
 * 日期: 2024-10-12 10:41:43
 * 备注: 用户信息表（字段不能设置默认值，代码里设置）
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserInfoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected String userName = ""; // 用户名称
    protected int serverId; // 服务器ID
    protected byte vip; // vip等级
    protected int vipScore; // vip积分
    protected int freeVipScore; // 免费vip积分
    protected String icon = ""; // 头像
    protected long iconCdtime; // 头像上传CD时间
    protected int location; // 玩家的地理位置
    protected int level; // 等级
    protected int repu; // 声望 (提升主角等级)
    protected int diamond; // 钻石
    protected long gold; // 金币
    protected long loginTime; // 最后登录时间
    protected byte language; // 语言类型
    protected int iconFrame; // 玩家的头像框
    protected int energy; // 城主体力
    protected long energyRecoverTime; // 上次体力恢复时间
    protected int stageKey; // 主线关卡组合成的key
    protected long maxPower; // 历史最高战力
    protected String vipReward = ""; // vip等级领取奖励 (已领取)
    protected String firstChargeReward = ""; // 限时三日礼包奖励 (已领取 [第几天,第几天...])
    protected long isFirstCharge; // 是否购买限时三日礼包 (购买时间)
    protected String distinctId = ""; // 数数sdk账号id
    protected String timezone = ""; // 时区
    protected int titleId; // 佩戴的称号id
    protected int bubbleId; // 聊天气泡的id

    public void reset() {
    }

    protected String getKey() {
        return "UserInfo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_info"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入库
     */
    public JsonObject updateNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE_NOW, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("userName", userName); // 用户名称
        obj.put("serverId", serverId); // 服务器ID
        obj.put("vip", vip); // vip等级
        obj.put("vipScore", vipScore); // vip积分
        obj.put("freeVipScore", freeVipScore); // 免费vip积分
        obj.put("icon", icon); // 头像
        obj.put("iconCdtime", iconCdtime); // 头像上传CD时间
        obj.put("location", location); // 玩家的地理位置
        obj.put("level", level); // 等级
        obj.put("repu", repu); // 声望 (提升主角等级)
        obj.put("diamond", diamond); // 钻石
        obj.put("gold", gold); // 金币
        obj.put("loginTime", loginTime); // 最后登录时间
        obj.put("language", language); // 语言类型
        obj.put("iconFrame", iconFrame); // 玩家的头像框
        obj.put("energy", energy); // 城主体力
        obj.put("energyRecoverTime", energyRecoverTime); // 上次体力恢复时间
        obj.put("stageKey", stageKey); // 主线关卡组合成的key
        obj.put("maxPower", maxPower); // 历史最高战力
        obj.put("vipReward", vipReward); // vip等级领取奖励 (已领取)
        obj.put("firstChargeReward", firstChargeReward); // 限时三日礼包奖励 (已领取 [第几天,第几天...])
        obj.put("isFirstCharge", isFirstCharge); // 是否购买限时三日礼包 (购买时间)
        obj.put("distinctId", distinctId); // 数数sdk账号id
        obj.put("timezone", timezone); // 时区
        obj.put("titleId", titleId); // 佩戴的称号id
        obj.put("bubbleId", bubbleId); // 聊天气泡的id
        return obj;
    }

    public PBUserInfo.Builder takeInitBuilder() {
        PBUserInfo.Builder sb = PBUserInfo.newBuilder();
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (vip != 0) {
            sb.setVip(vip);
        }
        if (vipScore != 0) {
            sb.setVipScore(vipScore);
        }
        if (freeVipScore != 0) {
            sb.setFreeVipScore(freeVipScore);
        }
        if (!icon.isEmpty()) {
            sb.setIcon(icon);
        }
        if (iconCdtime != 0) {
            sb.setIconCdtime(iconCdtime);
        }
        if (location != 0) {
            sb.setLocation(location);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        if (repu != 0) {
            sb.setRepu(repu);
        }
        if (diamond != 0) {
            sb.setDiamond(diamond);
        }
        if (gold != 0) {
            sb.setGold(gold);
        }
        if (iconFrame != 0) {
            sb.setIconFrame(iconFrame);
        }
        if (energy != 0) {
            sb.setEnergy(energy);
        }
        if (energyRecoverTime != 0) {
            sb.setEnergyRecoverTime(energyRecoverTime);
        }
        if (this.vipReward != null && this.vipReward.length() > 0) {
            JsonArray list = new JsonArray(this.vipReward);
            for(int i=0; i<list.size(); i++) {
                sb.addVipReward(list.getInteger(i));
            }
        }
        if (this.firstChargeReward != null && this.firstChargeReward.length() > 0) {
            JsonArray list = new JsonArray(this.firstChargeReward);
            for(int i=0; i<list.size(); i++) {
                sb.addFirstChargeReward(list.getInteger(i));
            }
        }
        if (isFirstCharge != 0) {
            sb.setIsFirstCharge(isFirstCharge);
        }
        if (titleId != 0) {
            sb.setTitleId(titleId);
        }
        if (bubbleId != 0) {
            sb.setBubbleId(bubbleId);
        }
        return sb;
    }

    public PBUserInfo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserInfo.Builder sb = PBUserInfo.newBuilder();
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("vip")) {
            sb.setVip(vip);
        }
        if (isFieldUpdate("vipScore")) {
            sb.setVipScore(vipScore);
        }
        if (isFieldUpdate("freeVipScore")) {
            sb.setFreeVipScore(freeVipScore);
        }
        if (isFieldUpdate("icon")) {
            sb.setIcon(icon);
        }
        if (isFieldUpdate("iconCdtime")) {
            sb.setIconCdtime(iconCdtime);
        }
        if (isFieldUpdate("location")) {
            sb.setLocation(location);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        if (isFieldUpdate("repu")) {
            sb.setRepu(repu);
        }
        if (isFieldUpdate("diamond")) {
            sb.setDiamond(diamond);
        }
        if (isFieldUpdate("gold")) {
            sb.setGold(gold);
        }
        if (isFieldUpdate("iconFrame")) {
            sb.setIconFrame(iconFrame);
        }
        if (isFieldUpdate("energy")) {
            sb.setEnergy(energy);
        }
        if (isFieldUpdate("energyRecoverTime")) {
            sb.setEnergyRecoverTime(energyRecoverTime);
        }
        if (isFieldUpdate("vipReward")) {
            if (this.vipReward != null && this.vipReward.length() > 0) {
                JsonArray list = new JsonArray(this.vipReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addVipReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearVipReward();
                }
            } else {
                sb.clearVipReward();
            }
        }
        if (isFieldUpdate("firstChargeReward")) {
            if (this.firstChargeReward != null && this.firstChargeReward.length() > 0) {
                JsonArray list = new JsonArray(this.firstChargeReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addFirstChargeReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearFirstChargeReward();
                }
            } else {
                sb.clearFirstChargeReward();
            }
        }
        if (isFieldUpdate("isFirstCharge")) {
            sb.setIsFirstCharge(isFirstCharge);
        }
        if (isFieldUpdate("titleId")) {
            sb.setTitleId(titleId);
        }
        if (isFieldUpdate("bubbleId")) {
            sb.setBubbleId(bubbleId);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public byte getVip() {
        return vip;
    }

    public void putVip(byte vip) {
        if (this.vip != vip) {
            this.vip = vip;
            updateRedisHashField(getKey(), "vip", this.vip);
        }
    }

    public void addVip(byte value) {
        this.vip += value;
        updateRedisHashField(getKey(), "vip", this.vip);
    }

    public int getVipScore() {
        return vipScore;
    }

    public void putVipScore(int vipScore) {
        if (this.vipScore != vipScore) {
            this.vipScore = vipScore;
            updateRedisHashField(getKey(), "vipScore", this.vipScore);
        }
    }

    public void addVipScore(int value) {
        this.vipScore += value;
        updateRedisHashField(getKey(), "vipScore", this.vipScore);
    }

    public int getFreeVipScore() {
        return freeVipScore;
    }

    public void putFreeVipScore(int freeVipScore) {
        if (this.freeVipScore != freeVipScore) {
            this.freeVipScore = freeVipScore;
            updateRedisHashField(getKey(), "freeVipScore", this.freeVipScore);
        }
    }

    public void addFreeVipScore(int value) {
        this.freeVipScore += value;
        updateRedisHashField(getKey(), "freeVipScore", this.freeVipScore);
    }

    public String getIcon() {
        return icon;
    }

    public void putIcon(String icon) {
        if (!this.icon.equals(icon)) {
            this.icon = icon;
            updateRedisHashField(getKey(), "icon", this.icon);
        }
    }

    public long getIconCdtime() {
        return iconCdtime;
    }

    public void putIconCdtime(long iconCdtime) {
        if (this.iconCdtime != iconCdtime) {
            this.iconCdtime = iconCdtime;
            updateRedisHashField(getKey(), "iconCdtime", this.iconCdtime);
        }
    }

    public void addIconCdtime(long value) {
        this.iconCdtime += value;
        updateRedisHashField(getKey(), "iconCdtime", this.iconCdtime);
    }

    public int getLocation() {
        return location;
    }

    public void putLocation(int location) {
        if (this.location != location) {
            this.location = location;
            updateRedisHashField(getKey(), "location", this.location);
        }
    }

    public void addLocation(int value) {
        this.location += value;
        updateRedisHashField(getKey(), "location", this.location);
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

    public int getRepu() {
        return repu;
    }

    public void putRepu(int repu) {
        if (this.repu != repu) {
            this.repu = repu;
            updateRedisHashField(getKey(), "repu", this.repu);
        }
    }

    public void addRepu(int value) {
        this.repu += value;
        updateRedisHashField(getKey(), "repu", this.repu);
    }

    public int getDiamond() {
        return diamond;
    }

    public void putDiamond(int diamond) {
        if (this.diamond != diamond) {
            this.diamond = diamond;
            updateRedisHashField(getKey(), "diamond", this.diamond);
        }
    }

    public void addDiamond(int value) {
        this.diamond += value;
        updateRedisHashField(getKey(), "diamond", this.diamond);
    }

    public long getGold() {
        return gold;
    }

    public void putGold(long gold) {
        if (this.gold != gold) {
            this.gold = gold;
            updateRedisHashField(getKey(), "gold", this.gold);
        }
    }

    public void addGold(long value) {
        this.gold += value;
        updateRedisHashField(getKey(), "gold", this.gold);
    }

    public long getLoginTime() {
        return loginTime;
    }

    public void putLoginTime(long loginTime) {
        if (this.loginTime != loginTime) {
            this.loginTime = loginTime;
            updateRedisHashField(getKey(), "loginTime", this.loginTime);
        }
    }

    public void addLoginTime(long value) {
        this.loginTime += value;
        updateRedisHashField(getKey(), "loginTime", this.loginTime);
    }

    public byte getLanguage() {
        return language;
    }

    public void putLanguage(byte language) {
        if (this.language != language) {
            this.language = language;
            updateRedisHashField(getKey(), "language", this.language);
        }
    }

    public void addLanguage(byte value) {
        this.language += value;
        updateRedisHashField(getKey(), "language", this.language);
    }

    public int getIconFrame() {
        return iconFrame;
    }

    public void putIconFrame(int iconFrame) {
        if (this.iconFrame != iconFrame) {
            this.iconFrame = iconFrame;
            updateRedisHashField(getKey(), "iconFrame", this.iconFrame);
        }
    }

    public void addIconFrame(int value) {
        this.iconFrame += value;
        updateRedisHashField(getKey(), "iconFrame", this.iconFrame);
    }

    public int getEnergy() {
        return energy;
    }

    public void putEnergy(int energy) {
        if (this.energy != energy) {
            this.energy = energy;
            updateRedisHashField(getKey(), "energy", this.energy);
        }
    }

    public void addEnergy(int value) {
        this.energy += value;
        updateRedisHashField(getKey(), "energy", this.energy);
    }

    public long getEnergyRecoverTime() {
        return energyRecoverTime;
    }

    public void putEnergyRecoverTime(long energyRecoverTime) {
        if (this.energyRecoverTime != energyRecoverTime) {
            this.energyRecoverTime = energyRecoverTime;
            updateRedisHashField(getKey(), "energyRecoverTime", this.energyRecoverTime);
        }
    }

    public void addEnergyRecoverTime(long value) {
        this.energyRecoverTime += value;
        updateRedisHashField(getKey(), "energyRecoverTime", this.energyRecoverTime);
    }

    public int getStageKey() {
        return stageKey;
    }

    public void putStageKey(int stageKey) {
        if (this.stageKey != stageKey) {
            this.stageKey = stageKey;
            updateRedisHashField(getKey(), "stageKey", this.stageKey);
        }
    }

    public void addStageKey(int value) {
        this.stageKey += value;
        updateRedisHashField(getKey(), "stageKey", this.stageKey);
    }

    public long getMaxPower() {
        return maxPower;
    }

    public void putMaxPower(long maxPower) {
        if (this.maxPower != maxPower) {
            this.maxPower = maxPower;
            updateRedisHashField(getKey(), "maxPower", this.maxPower);
        }
    }

    public void addMaxPower(long value) {
        this.maxPower += value;
        updateRedisHashField(getKey(), "maxPower", this.maxPower);
    }

    public JsonArray getVipRewardArray() {
        if (this.vipReward != null && this.vipReward.length() > 0) {
            return new JsonArray(this.vipReward);
        } else {
            return new JsonArray();
        }
    }

    public String getVipReward() {
        return vipReward;
    }

    public void putVipReward(String vipReward) {
        if (!this.vipReward.equals(vipReward)) {
            this.vipReward = vipReward;
            updateRedisHashField(getKey(), "vipReward", this.vipReward);
        }
    }

    public JsonArray getFirstChargeRewardArray() {
        if (this.firstChargeReward != null && this.firstChargeReward.length() > 0) {
            return new JsonArray(this.firstChargeReward);
        } else {
            return new JsonArray();
        }
    }

    public String getFirstChargeReward() {
        return firstChargeReward;
    }

    public void putFirstChargeReward(String firstChargeReward) {
        if (!this.firstChargeReward.equals(firstChargeReward)) {
            this.firstChargeReward = firstChargeReward;
            updateRedisHashField(getKey(), "firstChargeReward", this.firstChargeReward);
        }
    }

    public long getIsFirstCharge() {
        return isFirstCharge;
    }

    public void putIsFirstCharge(long isFirstCharge) {
        if (this.isFirstCharge != isFirstCharge) {
            this.isFirstCharge = isFirstCharge;
            updateRedisHashField(getKey(), "isFirstCharge", this.isFirstCharge);
        }
    }

    public void addIsFirstCharge(long value) {
        this.isFirstCharge += value;
        updateRedisHashField(getKey(), "isFirstCharge", this.isFirstCharge);
    }

    public String getDistinctId() {
        return distinctId;
    }

    public void putDistinctId(String distinctId) {
        if (!this.distinctId.equals(distinctId)) {
            this.distinctId = distinctId;
            updateRedisHashField(getKey(), "distinctId", this.distinctId);
        }
    }

    public String getTimezone() {
        return timezone;
    }

    public void putTimezone(String timezone) {
        if (!this.timezone.equals(timezone)) {
            this.timezone = timezone;
            updateRedisHashField(getKey(), "timezone", this.timezone);
        }
    }

    public int getTitleId() {
        return titleId;
    }

    public void putTitleId(int titleId) {
        if (this.titleId != titleId) {
            this.titleId = titleId;
            updateRedisHashField(getKey(), "titleId", this.titleId);
        }
    }

    public void addTitleId(int value) {
        this.titleId += value;
        updateRedisHashField(getKey(), "titleId", this.titleId);
    }

    public int getBubbleId() {
        return bubbleId;
    }

    public void putBubbleId(int bubbleId) {
        if (this.bubbleId != bubbleId) {
            this.bubbleId = bubbleId;
            updateRedisHashField(getKey(), "bubbleId", this.bubbleId);
        }
    }

    public void addBubbleId(int value) {
        this.bubbleId += value;
        updateRedisHashField(getKey(), "bubbleId", this.bubbleId);
    }

}